import { addCategory, updateCategory, queryCategory } from '../services/api';
import { message } from 'antd';
export default {
  namespace: 'category',

  state: {
    modal: false,
    list: [],
    item: {},
    loadding: false
  },
  effects: {
    *fetchCategory(_, { call, put }) {
      const response = yield call(queryCategory);
      yield put({
        type: 'queryList',
        payload: response.list,
      });
    },
    *saveCategory({ payload }, { call, put }) {
      console.log(`payload:${JSON.stringify(payload)}`)
      let response = yield call(addCategory, payload);
      if (response.status == 'ok') {
        message.success("保存成功");
        yield put({
          type: 'changeModal',
          payload: {
            modal: false,
            item: {}
          }
        });
      } else {
        message.error(response.msg);
      }
    },
    *closeEdit(_, { call, put }) {
      yield put({
        type: 'changeModal',
        payload: { modal: false, item: {} }
      })
    },
    *changeEdit({ payload }, { call, put }) {
      yield put({
        type: 'changeModal',
        payload: { modal: true, item: payload }
      })
    }
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    changeModal(state, action) {
      return {
        ...state,
        modal: action.payload.modal,
        item: action.payload.item || {}
      }
    }
  }
};
